<?php
declare (strict_types = 1);

namespace app\admin\model;

use app\admin\service\AdminService;
use app\admin\service\ConfigService;
use think\Model;

/**
 * 后台登录日志
 *
 * @Author tanruizheng
 * @DateTime 2021-07-09
 */
class OperationLogModel extends Model
{
    //表名
    protected $name = 'operation_log';

    /**
     * 记录后台操作日志
     *
     * @Author tanruizheng
     * @DateTime 2021-07-09
     * @param [type] $username
     * @param [type] $logintime
     * @param [type] $loginip
     * @param [type] $status
     * @param [type] $password
     * @param [type] $info
     * @return void
     */
    public static function createOperationLog(\think\Response $request){
        //判断是否开启操作日志记录开关
        $isOperationLog = ConfigService::getConfig('is_operation_log')['data'];
        if(!$isOperationLog) return false;
        //当前登录用户信息
        $adminInfo = AdminService::instance()->getAdminInfo();
        //操作内容
        $content = json_decode($request->getContent(), true);
        $model = new self();
        $model->uid = $adminInfo['id'];//管理员ID
        $model->time = time();//时间
        $model->ip = request()->ip();//IP
        $model->status = $content['status'] ? 1 : 0;//状态,0错误提示，1为正确提示
        $model->get = request()->url();
        $model->info = $request->getContent();//其他说明
        $model->save();
        return true;
    }
}
